/**
 * Created with JetBrains PhpStorm.
 * User: Administrator
 * Date: 18-1-20
 * Time: 下午5:57
 * To change this template use File | Settings | File Templates.
 */
var http = {
    baseUrl: "http://192.168.71.21:20080/rsa/",//测试地址
    publicKey: "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0WbcJT1sdyXlI3tlLZUlARxLNvoGOtUvQtTGkyJT76VBZyMYcOXyV+7bJ0OE3p6d0Au8vIU4LVRwXcNYISjdSOPCC43uETjZMEp1O8HQvcm0yqbqr+oByXfVviTiRTReVsAP2MikyrCnwpnEJRdZ+sBunJ2NVwH/uD+npSyT2uwIDAQAB",
    //加密请求参数
    encrypt: function(obj) {
        var rsa = new JSEncrypt();
        console.log(obj);
        var data = JSON.stringify(obj);
        console.log(data);

        if(data.length> 2691){return;} // length limit
        rsa.setPublicKey(this.publicKey);
        crypt_res = "";
        for(var index=0; index < (data.length - data.length%117)/117+1 ; index++)
        {
            var subdata = data.substr(index * 117,117);
            crypt_res += rsa.encrypt(subdata);
        }
        return crypt_res;

    },
    //发送ajax请求
    ajax: function(obj) {
        var config = {
            type: obj.type,
            param: {},
            url: "",
            success: function(data) {
                console.log(data);
            },
            error: function(data) {
                alert(data.msg);
            }
        };
        $.extend(true, config, obj); //得到最终的传递的参数
        var thiz = this,
            _param = thiz.encrypt(config.param); //加密传递的参数

        if(config.type == "GET"){
            $.ajax({
                type: config.type,
                url: thiz.baseUrl + config.url,
                data: {
                    "secret": _param
                },
                dataType: "jsonp",
                jsonp: "callback", //传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名(一般默认为:callback)
                success: function(data) {
                    if (parseInt(data.status) == 0) {
                        config.success(data);
                    } else {
                        if (config.error) {
                            config.error(data);
                        }
                    }
                },
                error: function(data) {
                    if (config.errorCb) {
                        config.errorCb(data);
                    }
                }
            });
        }else{

            $.ajax({
                type: "POST",//提交方式
                url: thiz.baseUrl + config.url,//提交地址
                data: {"secret": _param},//将表单全部提交
                success: function(data, textStatus, request) {
                    if (parseInt(data.status) == 0) {
                        config.success(data);
                    } else {
                        if (config.error) {
                            config.error(data);
                        }
                    }
                },
                error: function(data) {
                    if (config.error) {
                        config.error(data);
                    }
                }
            });
        }

    }
}